package de.preisrobot.service.helper;

import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientFactory {

	public static HttpClient getHttpClient() {

		HttpClient client = new HttpClient();
		client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
		client.getParams().setParameter(
				HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

		client.getHostConfiguration();

		return client;
	}

	public static GetMethod createGETMethod(String sourceUrlString) {

		GetMethod method = new GetMethod(sourceUrlString);
		method.getParams()
				.setParameter(
						HttpMethodParams.USER_AGENT,
						"Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18");
		// RFC 2101 cookie management spec is used per default
		// to parse, validate, format & match cookies
		method.getParams()
				.setParameter("http.protocol.handle-redirects", false);

		return method;
	}

	public static PostMethod createPOSTMethod(String sourceUrlString,
			String paramName, String paramValue) throws  Exception {

		PostMethod method = new PostMethod(sourceUrlString);
		method.getParams()
				.setParameter(
						HttpMethodParams.USER_AGENT,
						"Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18");
		// RFC 2101 cookie management spec is used per default
		// to parse, validate, format & match cookies
		method.getParams()
				.setParameter("http.protocol.handle-redirects", false);
		method.addParameter(paramName, new String(paramValue.getBytes(), "UTF-8"));

		return method;
	}

	public static PostMethod createPOSTObjectMethod(String sourceUrlString,
			String paramName, String paramValue) throws  Exception {

		PostMethod method = new PostMethod(sourceUrlString);
		method.getParams()
				.setParameter(
						HttpMethodParams.USER_AGENT,
						"Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18");
	 
		// RFC 2101 cookie management spec is used per default
		// to parse, validate, format & match cookies
		method.getParams()
				.setParameter("http.protocol.handle-redirects", false);
		method.addParameter(paramName, new String(paramValue.getBytes(), "UTF-8"));

		return method;
	}

}
